package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.entity.Customer;
import com.qf.service.CustomerService;
import com.qf.vo.MessageVo;
import com.qf.vo.PageVo;
import com.qf.vo.tableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

//    @RequestMapping("/getAllCustomer")
//    @ResponseBody
//    public tableVo<Customer> test1(PageVo pageVo){
//        PageInfo<Customer> pageInfo=customerService.findAll(pageVo);
//        tableVo<Customer> table=new tableVo<>();
//        table.setCount(pageInfo.getTotal());
//        table.setCode("0");
//        table.setMsg("success");
//        table.setData(pageInfo.getList());
//        System.out.println(table);
//        return table;
//    }

    @RequestMapping("/getAllCustomer")
    @ResponseBody
    public tableVo<Customer> test0(PageVo pageVo,Customer customer){
        PageInfo<Customer> pageInfo=customerService.findAll(pageVo,customer);
        tableVo<Customer> table=new tableVo<>();
        table.setCount(pageInfo.getTotal());
        table.setCode("0");
        table.setMsg("success");
        table.setData(pageInfo.getList());
        System.out.println(table);
        return table;
    }

    @RequestMapping("/add")
    @ResponseBody
    public MessageVo test2(Customer customer){
        int a=customerService.insertCustomer(customer);
        MessageVo message=new MessageVo();
        message.setStatus(a==1?true:false);
        message.setMessage(a==1?"success":"fail");
        message.setData(customer);
        return message;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public MessageVo test7(Integer[] ids){
        Integer a=customerService.deleteUser(ids);
        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a>0?"success":"fail");
//        message.setData(user);
        return message;
    }

    @RequestMapping("/update")
    @ResponseBody
    public MessageVo test8(Customer customer){
        Integer a= customerService.updateCustomer(customer);
        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a>0?"success":"fail");
        return message;
    }

}
